package com.leyou.item.controller;

import com.leyou.item.dto.*;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**

 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService spuDetailService;
    /**
     * 分页查询spu
     * @param page 当前页码
     * @param rows 每页显示记录数
     * @param brandId 品牌id
     * @param categoryId 商品分类id
     * @param id spu的id
     * @param saleable 是否上架
     */
    @GetMapping("/spu/page")
    private ResponseEntity<PageDTO<SpuDTO>> queryPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ) {
        return ResponseEntity.ok(spuService.queryPage(page, rows,brandId,categoryId,id,saleable));
    }



    /**
     * 根据商品id（spuId）查询对应的规格参数组集合以及组内参数的信息，及对应值
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpec(
            @RequestParam("id")Long id,
            @RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(this.spuService.listSpec(id,searching));
    }


    /**
     * 根据id批量查询sku
     * @param ids
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(skuService.querySkuListByIds(ids));
    }

    /**
     * 根据spuId查询sku集合
     * @param cid
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuId(@RequestParam("id") Long cid){
        return ResponseEntity.ok(skuService.querySkuListBySpuId(cid));
    }


    /**
     * 根据id查询商品详情
     * @param cid
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailById(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(spuDetailService.queryDetailById(cid));
    }


    /**
     * 根据id查询商品
     * @param cid
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long cid) {
        return ResponseEntity.ok(spuService.querySpuById(cid));
    }


    /**
     * 根据id查询spu及sku、spuDetail等
     * @param spuId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoods(@PathVariable("id") Long spuId) {
        return ResponseEntity.ok(spuService.queryGoods(spuId));
    }


    /**
     * 商品新增
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO){
        spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 修改商品上下架
     * @param cid
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> saleableById(
            @RequestParam(value = "id",required = false) Long cid,
            @RequestParam(value = "saleable",required = false) Boolean saleable) {
        this.spuService.saleableById(cid, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 更新商品
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        this.spuService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }



}
